#include "vfx_whiteout.h"

#include "bones/biped_instance.h"
#include "graphics/graphics.h"
#include "ui/ui_sprite.h"

#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    

#include <cassert>

using namespace vfx;


bool VFXWhiteOut::m_Started = false;

void VFXWhiteOut::Render(ui::UISprite* pSprite)
{
	assert(pSprite);

	if (Begin())
	{
		pSprite->Render();
		End();
	}
}

void VFXWhiteOut::Render(bones::BipedInstance* pBiped, const math::Vec2& Offset)
{
	assert(pBiped);

	if (Begin())
	{
		pBiped->Render(Offset);
		End();
	}
}

bool VFXWhiteOut::Begin()
{
	assert(!m_Started);

#ifdef _PLATFORM_WIN32
	GetGraphics()->SetTexStage(0, Graphics::COLOR_OP, Graphics::BLEND_SELECTARG2);
#endif
#ifdef _PLATFORM_WII
	GXSetTevColorIn(GX_TEVSTAGE0, GX_CC_RASC, GX_CC_ZERO, GX_CC_ZERO,GX_CC_ZERO);
	GXSetTevColorOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVPREV);
#endif

	m_Started = true;
	return true;
}

void VFXWhiteOut::End()
{
	assert(m_Started);

#ifdef _PLATFORM_WIN32
	GetGraphics()->SetTexStage(0, Graphics::COLOR_OP, Graphics::BLEND_MODULATE);
#endif
#ifdef _PLATFORM_WII
	GXSetTevColorIn(GX_TEVSTAGE0, GX_CC_TEXC, GX_CC_RASC, GX_CC_ZERO,GX_CC_ZERO);
	GXSetTevColorOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVPREV);
#endif

	m_Started = false;
}
